<template>
  <div style="background-color: white">
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="from"
      :list="formList"
      :cols="4"
      @formChange="changeHandle"
      :isSubmitBtn="false"
      labelWidth="110" />
    <BreakSpace label="工单信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
       ref="billfrom"
      :list="billList"
      :cols="4"
      :formType="type"
      @formChange="changeHandleBill"
      :isSubmitBtn="false"
      labelWidth="110" />
    <BreakSpace label="车辆信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="car"
      :list="carList"
      :cols="4"
      :formType="type"
      @formChange="changeHandleCar"
      :isSubmitBtn="false"
      labelWidth="110" />
    <BreakSpace label="投诉调查" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="query"
      :list="queryList"
      :cols="3"
      @formChange="changeHandlequery"
      :isSubmitBtn="false"
      labelWidth="110" />
    <BreakSpace label="处理明细" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
     ref="table"
     height="auto"
     :isColumnFilter="false"
     :isPagination="false"
     :isToperInfo="false"
     :columns="columns"
     :dataSource="complaindealwithlist"
     :isSelectColumn="false"
     columnsRef="jzySeries"
     :onColumnsChange="columns => this.columns = columns"
    >
    </FilterTable>
    <BreakSpace label="结案信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel
      ref="closecase"
      :list="closecaseList"
      :cols="4"
      :isSubmitBtn="false"
      labelWidth="110" />
    <BreakSpace label="历史审核记录" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table1"
      height="auto"
      :isColumnFilter="false"
      :isPagination="false"
      :isToperInfo="false"
      :columns="columns1"
      :fetchapi="fetchApi1"
      :params="fetchParams1"
      :dataSource="arbitrationList"
      :isSelectColumn="false"
      columnsRef="jzySeries"
      :onColumnsChange="columns => this.columns1 = columns1"
    >
    </FilterTable>
    <BaseDialog
      :visible.sync="istibao"
      :title="'申请仲裁'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <tibao  @close="closeTibao" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="isarbitration"
      :title="'提报'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <arbitration  @close="closeArbitration" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="detil"
      :title="'处理明细'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <dealwithdetail :type="handleType" :rowData="formData" @close="closedetil" />
    </BaseDialog>
    <div class="dialogBottmView">
      <el-button  :loading="normalLoading" @click="closecase(1)"  type="primary">正常结案</el-button>
      <el-button  :loading="forceLoading" @click="closecase(2)"  type="primary">强制结案</el-button>
      <el-button   @click="closeDialog"  type="primary">返回</el-button>
    </div>
  </div>
</template>

<script>
  import moment from 'moment'
  import tibao from './tibao';
  import arbitration from './arbitration';
  import {doupdate} from '@/api/customerService/complaint/complaintbill/index.js';
  import {getcomplaindetiallist,doupdateforclosecase,getarbitrationlist} from '@/api/customerService/complaint/complaintbill/index.js';
  import {
    complaintSerious,
    jaStatus,
    isFeedback,
    SySubtype,
    complaintGender,
    complaintType,
    status,
    sjtype,
    complaintOrigin,
    shtype
  } from '../complaintbill/complaint';
  import { dictionary } from '@/utils/dictMixin';
  import { region } from '@/api/cms-common/region';
  import dealwithdetail from './dealwithdetail';
    export default {
      mixins: [dictionary,region],
      name: "complaindetil",
      props: {
        type: {
          type: String,
          default: 'add'
        },
        rowData: {
          type: Object,
          default: () => ({})
        }
      },
      components: {
        tibao,arbitration,dealwithdetail
      },
      data() {
        this.saveData=null;
        return{
          detil:false,
          handleType:'',
          forceLoading:false,
          normalLoading:false,
          istibao:false,
          isarbitration:false,
          complaindealwithlist:[],
          roNo:'',// 工单号
          roCreateDate:'',// 开单日期
          formData:{},
          saveLoading:false,
          showAddNew:true,
          fetchParams:{},
          fetchParams1:{},
          arbitrationList:[],
          columns1:this.arbitrationTableColumns(),
          fetchApi1:this.getarbitrationlist,
          columns: this.createTableColumns(),
          billList:this.createTopFilterList(2),
          formList: this.createTopFilterList(1),
          carList:this.createTopFilterList(3),
          queryList:this.createTopFilterList(4),
          closecaseList:this.createTopFilterList(5),
          dealerId:'', //经销商的id
          dealerCode:'', //经销商代码
          rono:'', //订单号
          ronoId:'',// 订单的ID
          vin:''
        };
      },
      created() {
        this.fetchParams.id=this.rowData.id
        this.init()
      },
      methods: {
        // 获取审核历史列表
        getarbitrationlist(){
          getarbitrationlist({id:this.rowData.id}).then(res=>{
            this.arbitrationList=res.data.records
          })
        },
        // 审批历史列表
        arbitrationTableColumns(){
          return [
            {
              title: '序号',
              dataIndex: 'index',
              width: 70,
              sorter: true,
              render: props => {
                return <span>{props.row.index + 1}</span>;
              }
            },
            {
              title: '审核部门',
              dataIndex: 'complainBranch',
              sorter: true,
            },
            {
              title: '审核人',
              dataIndex: 'complainName',
              sorter: true,
            },
            {
              title: '审核状态',
              dataIndex: 'complainStatus',
              sorter: true,
              dictItems: shtype
            },
            {
              title: '审核时间',
              dataIndex: 'complainTime',
              dateFormat:'yyyy-MM-dd HH:mm:ss',
              sorter: true,
            },
            {
              title: '驳回原因',
              dataIndex: 'rejectReason',
              sorter: true,
            },
            {
              title: '通过原因',
              dataIndex: 'adoptReason',
              sorter: true,
            }
          ]
        },
        //保存结案信息
          doclosecase(type){
            if (type==='QZ'){
              this.forceLoading = true
            }else{
              this.normalLoading=true
            }
            this.rowData.complaintDate =  moment(this.rowData.complaintDate).format('YYYY-MM-DD HH:mm:ss')
            doupdateforclosecase(this.rowData).then(res=>{
              if (res.resultCode === 200) {
                this.$notify.success({title: '提示信息',message:'保存成功!'});
                // 重新加载
                this.closeDialog();
              }else {
                this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
                this.closeDialog();
              }
            })
          } ,
         // 正常结案关闭按钮
        closeTibao(params){
          if (params){
            const base=this.$refs.closecase.SUBMIT_FORM();
            Object.assign(this.rowData,params)
            this.rowData.completeRemark=params.completeRemark
            this.rowData.isFeedback=base.isFeedback
            this.rowData.jaStatus='D'// 强制结案
            console.info(this.rowData)
            this.doclosecase('QZ')
          }
          this.istibao=false
        },
        // 正常结案关闭按钮
        closeArbitration(params){
          if (params){
            const base=this.$refs.closecase.SUBMIT_FORM();
            Object.assign(this.rowData,params)
            const type = base.isFeedback
            this.rowData.isFeedback=type
            if (type==='Y'){
              this.rowData.jaStatus='C'// 正常结案不可回访
            }else{
              this.rowData.jaStatus='E'// 正常结案不可回访
            }
            console.info(this.rowData)
            this.doclosecase('ZC')
          }
          this.isarbitration=false
        },
        // 结案
        closecase(type){
          const base=this.$refs.closecase.SUBMIT_FORM();
          if (base){
            // 正常结案
            if (type===1){
              if (this.rowData.type==='CC'){
                if (base.isFeedback==='N'){
                  this.$notify.error({title: '提示信息',message:'正常结案回访状态必须选是!'});
                }else {
                  // 正常结案
                  this.isarbitration=true
                }
              }else{
                // 正常结案
                this.isarbitration=true
              }
            }else{
              if (this.rowData.type==='C'){
                this.$notify.error({title: '提示信息',message:'经销商自建工单不允许强制结案!'});
              }else{
                console.info(base)
                if (base.isFeedback==='Y'){
                  this.$notify.error({title: '提示信息',message:'强制结案回访状态必须是否!'});
                }else {
                  // 强制结案
                  this.istibao=true
                }
              }
            }
          }
        },
        getcomplaindetiallist() {
          getcomplaindetiallist({ id: this.rowData.id }).then(res => {
            this.complaindealwithlist = res.data.records
            this.complaindealwithlist.forEach(item=>{
              item.handleTime= moment(item.handleTime).format('YYYY-MM-DD HH:mm:ss')
            })
          })
        },
        async doupdate(){
           Object.assign(this.rowData,{complaindealwithlist:[]})
           this.rowData.complaindealwithlist=this.complaindealwithlist
           this.rowData.complaintDate =  moment(this.rowData.complaintDate).format('YYYY-MM-DD HH:mm:ss')
           this.saveLoading=true
           doupdate(this.rowData).then(res=>{
             if (res.resultCode === 200) {
               this.$notify.success({title: '提示信息',message:'保存成功!'});
               this.saveLoading=false
               this.closeDialog()
             }else {
               this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
               this.saveLoading=false
               this.closeDialog()
             }
           })
        },
        // 处理明细新增
        closedetil(){
          this.detil=false
        },
        dialogedit(row,type){
          this.handleType = type
          this.formData = row;
          this.detil=true
        },
        //  处理明细列表
        createTableColumns(){
          return [
            {
              title: '操作',
              dataIndex: 'column-action',
              width: 60,
              fixed: 'left',
              render: (props) => {
                return (
                  <div>
                    <el-button size="mini" type="text" onClick={() => this.dialogedit(props.row,'show')}>
                      明细
                    </el-button>
                  </div>
                );
              }
            },
            {
              title: '序号',
              dataIndex: 'index',
              width: 60,
              render: props => {
                return <span>{props.row.index + 1}</span>;
              }
            },
            {
              title: '处理时间',
              dataIndex: 'handleTime',
              dateFormat:'yyyy-MM-dd HH:mm:ss'
            },
            {
              title: '处理人',
              dataIndex: 'handleBy'
            },
            {
              title: '处理人联系方式',
              dataIndex: 'phone'
            },
            {
              title: '处理结果',
              dataIndex: 'content'
            },
            {
              title: '备注',
              dataIndex: 'remarks'
            }
          ]

        },
        //返回操作
        closeDialog() {
          this.$emit('close', { visible: false });
        },
        // 初始化列表带过来的一行数据
        init(){
          this.formList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
          this.queryList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
          this.billList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
          this.carList.forEach(x => {
            if (x.fieldName==='ownerProperty'){
              x.initialValue= this.createDictText( this.rowData[x.fieldName], '8011')
            }else{
              x.initialValue = this.rowData[x.fieldName];
            }
          });
          this.closecaseList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
          this.getcomplaindetiallist()
        },
        createTopFilterList(type) {
          if (type===1){
            return [{//2
              type: 'INPUT',
              label: '投诉单编号',
              fieldName: 'complaintNo',
              initialValue:'',
              disabled:true
            },
            {//3
              type: 'INPUT',
              label: '投诉人姓名',
              fieldName: 'complaintName',
              initialValue: '',
              disabled: true,
              placeholder: '请输入',
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {//3
              type: 'SELECT',
              label: '投诉人性别',
              fieldName: 'complaintGender',
              initialValue: '',
              placeholder: '',
              disabled: true,
              itemList:complaintGender,
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {//14
              type: 'INPUT',
              label: '投诉人电话',
              fieldName: 'complaintPhone',
              placeholder: '请输入',
              initialValue: '',
              disabled: true,
              maxlength:11,
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'blur'
                },
                {
                  pattern: /^1(3|4|5|6|7|8|9)\d{9}$/,
                  message: '请填写正确的手机格式',
                  trigger: 'blur'
                }
              ]
            },
            {//3
              type: 'SELECT',
              label: '投诉类型',
              fieldName: 'complaintType',
              initialValue: '',
              placeholder: '',
              disabled: true,
              itemList:complaintType,
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {//3
              type: 'SELECT',
              label: '投诉子类',
              fieldName: 'complaintSubtype',
              initialValue: '',
              placeholder: '',
              disabled: true,
              itemList:SySubtype,
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {//3
              type: 'DATE',
              label: '投诉日期',
              fieldName: 'complaintDate',
              format:'yyyy-MM-dd HH:mm:ss',
              initialValue: '',
              disabled: true,
              placeholder: '',
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {//3
              type: 'SELECT',
              label: '处理状态',
              fieldName: 'status',
              initialValue: '',
              placeholder: '',
              itemList:status,
              disabled: true
            },
            {
              type: 'INPUT',
              label: '接待员',
              fieldName: 'receptionist',
              initialValue: '',
              disabled:true
            },
            {//3
              type: 'SELECT',
              label: '数据来源',
              fieldName: 'type',
              initialValue: '',
              placeholder: '',
              itemList:sjtype,
              disabled: true,
            },
            {//3
              type: 'SELECT',
              label: '投诉来源',
              fieldName: 'complaintOrigin',
              initialValue: '',
              placeholder: '',
              disabled: true,
              itemList:complaintOrigin,
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {//3
              type: 'SELECT',
              label: '投诉等级',
              fieldName: 'complaintSerious',
              initialValue: '',
              placeholder: '',
              disabled: true,
              itemList:complaintSerious,
              rules: [
                {
                  required: true,
                  message: '必填',
                  trigger: 'change'
                }
              ]
            },
            {
              type: 'INPUT',
              label: '被投诉部门',
              fieldName: 'department',
              disabled: true,
              initialValue:'',
            },
            {
              type: 'INPUT',
              label: '被投诉员工',
              disabled: true,
              fieldName: 'beComplainTemp',
              initialValue:'',
            },
            {//22
              type: 'TEXT_AREA',
              label: '投诉单位',
              fieldName: 'complaintCorp',
              placeholder: '请输入',
              disabled: true,
              initialValue: '',
              maxlength:100,
              selfCols:4, //独占行数
            }
          ]
          }else if(type===2) {
            return[
              {//2
                type: 'INPUT',
                label: '工单号',
                fieldName: 'roNo',
                initialValue:'',
                readonly:true,
                unitRender: () => {
                  return (
                    <div>
                      <el-button icon="el-icon-search"></el-button>
                      <el-button slot="append" icon="el-icon-delete"></el-button>
                    </div>
                  );
                }
              },
              {//3
                type: 'DATE',
                label: '开单日期',
                fieldName: 'roCreateDate',
                format:'yyyy-MM-dd HH:mm:ss',
                initialValue: '',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '维修类型',
                fieldName: 'roType',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '服务顾问',
                fieldName: 'serviceAdvisor',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '送修人',
                fieldName: 'deliverer',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '送修人手机',
                fieldName: 'delivererMobile',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '送修人电话',
                fieldName: 'delivererPhone',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '责任技师',
                fieldName: 'chiefTechnician',
                initialValue:'',
                disabled:true
              }
            ]
          }else if (type===3){
            return[
              {//2
                type: 'INPUT',
                label: '车牌号',
                fieldName: 'license',
                initialValue:'',
                disabled:true,
                unitRender: () => {
                  return (
                    <div>
                      <el-button icon="el-icon-search" ></el-button>
                      <el-button slot="append" icon="el-icon-delete"></el-button>
                    </div>
                  );
                }
              },
              {//2
                type: 'INPUT',
                label: '车型',
                fieldName: 'modelName',
                initialValue:'',
                disabled:true,
              },
              {//3
                type: 'INPUT',
                label: 'VIN',
                fieldName: 'vin',
                initialValue: '',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '车主',
                fieldName: 'ownerName',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '车主性质',
                fieldName: 'ownerProperty',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'INPUT',
                label: '行驶里程',
                fieldName: 'currentMileage',
                initialValue:'',
                disabled:true
              },
              {//2
                type: 'DATE',
                label: '购车日期',
                fieldName: 'salesDate',
                format:'yyyy-MM-dd HH:mm:ss',
                initialValue:'',
                disabled:true
              },
            ]
          }else if (type===4){
            return [
              {//22
                type: 'TEXT_AREA',
                label: '投诉摘要',
                fieldName: 'complaintSummary',
                placeholder: '',
                selfCols:2,
                disabled:true,
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '必填',
                    trigger: 'blur'
                  }
                ]
              },
              {//22
                type: 'TEXT_AREA',
                label: '投诉原因',
                fieldName: 'complaintReason',
                selfCols:2,
                disabled:true,
                placeholder: '',
                initialValue: ''
              },
              {//22
                type: 'TEXT_AREA',
                label: '备注信息',
                fieldName: 'remark',
                selfCols:2,
                disabled:true,
                placeholder: '',
                initialValue: ''
              },
              {//22
                type: 'TEXT_AREA',
                label: '解决方案',
                fieldName: 'resolvent',
                selfCols:2,
                disabled:true,
                placeholder: '',
                initialValue: ''
              }
            ]
          }else{
            return [
              {//3
                type: 'SELECT',
                label: '结案状态',
                fieldName: 'jaStatus',
                initialValue: '',
                placeholder: '',
                disabled: true,
                itemList:jaStatus,
              },
              {//2
                type: 'TEXT_AREA',
                label: 'CC驳回原因',
                fieldName: 'ccRemark',
                selfCols:2,
                offsetRightCols:1,
                initialValue:'',
                disabled:true
              },
              {//3
                type: 'SELECT',
                label: '是否可回访',
                fieldName: 'isFeedback',
                initialValue: '',
                placeholder: '',
                itemList:isFeedback,
                rules: [
                  {
                    required: true,
                    message: '必填',
                    trigger: 'change'
                  }
                ]
              }
            ]
          }
        },
        changeHandle(val) {
          this.saveData=val;
        },
        changeHandleBill(val){
          console.info(val)
        },
        changeHandleCar(val){
          console.info(val)
        } ,
        changeHandlequery(val){
          console.info(val)
        }
      }
    }
</script>

<style scoped>

</style>
